#include "mydbcontrol.h"
#include "mydbexec.h"
#include <QSemaphore>
#define MAXCON 50
MyDBControl * MyDBControl::obj = nullptr;
MyDBControl::MyDBControl(QObject *parent) : QObject(parent)
  ,semaphore(new QSemaphore(MAXCON))
{
 for(int i=0; i < MAXCON ;i++)
 {
     execlist.append(new MyDBExec(QString("con_%1").arg(i)));
 }
}

MyDBExec *MyDBControl::getExec()
{
semaphore->acquire();
foreach (MyDBExec * mydbexec, execlist) {
if(mydbexec->getFreeState())
{
    mydbexec->setFreeState(false);
    return mydbexec;
}
}
semaphore->release();
return nullptr;
}

void MyDBControl::releaseExec(MyDBExec *exec)
{
if(exec == nullptr)
    return;
exec->setFreeState(true);
semaphore->release();
}

MyDBControl *MyDBControl::getObject()
{
 if(obj == nullptr)
 obj = new MyDBControl;
 return obj;
}
